package com.whale.activity;

import java.util.concurrent.Callable;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;

import com.whale.WhaleApplication;
import com.whale.api.IWebApiService;
import com.whale.entity.UserProfile;
import com.whale.util.async.ActivityUtils;
import com.whale.util.async.AsyncCallable;
import com.whale.util.async.Callback;
import com.whale.util.async.ProgressCallable;

/**
 * 
 * 类BaseActivity.java的实现描述：常用提取到这里
 * 
 * @author alibaba 2011-9-4 下午05:21:51
 */
public class BaseActivity extends Activity {
    
    /**
     * 获取远程服务接口
     */
    public IWebApiService getApiService() {
        return WhaleApplication.webApiService;
    }

    /**
     * 获取上下文
     * 
     * @return
     */
    public Context getContext() {
        return WhaleApplication.mContext;
    }

    public void showProgressDialog(String msg) {
        WhaleApplication.progressDialog.setMessage(msg);
        WhaleApplication.progressDialog.show();
    }

    public void cancelProgressDialog() {
        if (null != WhaleApplication.progressDialog) {
            WhaleApplication.progressDialog.cancel();
        }
    }

    public SharedPreferences getPreferences() {
        return WhaleApplication.mPreferences;
    }

 

    public static void setUserProfile(UserProfile userProfile) {
        WhaleApplication.userProfile = userProfile;
    }
    
    
	/**
	 * 默认显示“载入中”的提示框
	 * 
	 * @param <T>
	 *            模板参数，操作时要返回的内容
	 * @param pCallable
	 *            需要异步调用的操作
	 * @param pCallback
	 *            回调
	 */
	protected <T> void doAsync(final Callable<T> pCallable,
			final Callback<T> pCallback) {
		ActivityUtils.doAsync(this, null, "载入中...", true, pCallable, pCallback,
				null, false);
	}

	protected <T> void doAsync(boolean bShowPD, final Callable<T> pCallable,
			final Callback<T> pCallback) {
		ActivityUtils.doAsync(this, null, "载入中...", bShowPD, pCallable,
				pCallback, null, false);
	}

	/**
	 * 自定义文字的提示框
	 * 
	 * @param <T>
	 *            模板参数，操作时要返回的内容
	 * @param pTitle
	 *            提示框Title文字
	 * @param pMessage
	 *            提示框提示消息
	 * @param pCallable
	 *            需要异步调用的操作
	 * @param pCallback
	 *            回调
	 */
	protected <T> void doAsync(final CharSequence pTitle,
			final CharSequence pMessage, final Callable<T> pCallable,
			final Callback<T> pCallback) {
		ActivityUtils.doAsync(this, pTitle, pMessage, true, pCallable,
				pCallback, null, false);
	}

	protected <T> void doAsync(final CharSequence pTitle,
			final CharSequence pMessage, boolean bShowPD,
			final Callable<T> pCallable, final Callback<T> pCallback) {
		ActivityUtils.doAsync(this, pTitle, pMessage, bShowPD, pCallable,
				pCallback, null, false);
	}

	/**
	 * 自定义文字（采用资源文件中的文字）的提示框
	 * 
	 * @param <T>
	 *            模板参数，操作时要返回的内容
	 * @param pTitleResID
	 *            提示框Title文字Res
	 * @param pMessageResID
	 *            提示框提示消息Res
	 * @param pCallable
	 *            需要异步调用的操作
	 * @param pCallback
	 *            回调
	 */
	protected <T> void doAsync(final int pTitleResID, final int pMessageResID,
			final Callable<T> pCallable, final Callback<T> pCallback) {
		this.doAsync(pTitleResID, pMessageResID, true, pCallable, pCallback,
				null);
	}

	protected <T> void doAsync(final int pTitleResID, final int pMessageResID,
			final boolean bShowPD, final Callable<T> pCallable,
			final Callback<T> pCallback) {
		this.doAsync(pTitleResID, pMessageResID, bShowPD, pCallable, pCallback,
				null);
	}

	/**
	 * Performs a task in the background, showing a indeterminate
	 * {@link ProgressDialog}, while the {@link Callable} is being processed.
	 * 
	 * @param <T>
	 * @param pTitleResID
	 * @param pMessageResID
	 * @param pErrorMessageResID
	 * @param pCallable
	 * @param pCallback
	 * @param pExceptionCallback
	 */
	protected <T> void doAsync(final int pTitleResID, final int pMessageResID,
			final boolean bShowPD, final Callable<T> pCallable,
			final Callback<T> pCallback,
			final Callback<Exception> pExceptionCallback) {
		ActivityUtils.doAsync(this, pTitleResID, pMessageResID, bShowPD,
				pCallable, pCallback, pExceptionCallback);
	}

	/**
	 * Performs a task in the background, showing a {@link ProgressDialog} with
	 * an ProgressBar, while the {@link AsyncCallable} is being processed.
	 * 
	 * @param <T>
	 * @param pTitleResID
	 * @param pMessageResID
	 * @param pErrorMessageResID
	 * @param pAsyncCallable
	 * @param pCallback
	 */
	protected <T> void doProgressAsync(final int pTitleResID,
			final ProgressCallable<T> pCallable, final Callback<T> pCallback) {
		this.doProgressAsync(pTitleResID, pCallable, pCallback, null);
	}

	/**
	 * Performs a task in the background, showing a {@link ProgressDialog} with
	 * a ProgressBar, while the {@link AsyncCallable} is being processed.
	 * 
	 * @param <T>
	 * @param pTitleResID
	 * @param pMessageResID
	 * @param pErrorMessageResID
	 * @param pAsyncCallable
	 * @param pCallback
	 * @param pExceptionCallback
	 */
	protected <T> void doProgressAsync(final int pTitleResID,
			final ProgressCallable<T> pCallable, final Callback<T> pCallback,
			final Callback<Exception> pExceptionCallback) {
		ActivityUtils.doProgressAsync(this, pTitleResID, pCallable, pCallback,
				pExceptionCallback);
	}

	/**
	 * Performs a task in the background, showing an indeterminate
	 * {@link ProgressDialog}, while the {@link AsyncCallable} is being
	 * processed.
	 * 
	 * @param <T>
	 * @param pTitleResID
	 * @param pMessageResID
	 * @param pErrorMessageResID
	 * @param pAsyncCallable
	 * @param pCallback
	 * @param pExceptionCallback
	 */
	protected <T> void doAsync(final int pTitleResID, final int pMessageResID,
			final AsyncCallable<T> pAsyncCallable, final Callback<T> pCallback,
			final Callback<Exception> pExceptionCallback) {
		ActivityUtils.doAsync(this, pTitleResID, pMessageResID, pAsyncCallable,
				pCallback, pExceptionCallback);
	}
}
